const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
 
  },
  checkboxChange: function (e) { },
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  userPasswordInput: function (e) {
    this.setData({
      userPassword: e.detail.value
    })
  },
  logIn: function () {
    if (!this.data.userName || !this.data.userPassword) {
      wx.showToast({
        title: '请输入账号和密码',
        icon: "none"
      })
      return
    }
    app.reqFetch.getSignIN({
      username: this.data.userName,
      password: this.data.userPassword,
      type: 2
    }, (res) => {
      const {
        token,
        userid,
        name,
        username,
        realName,
        supplierName,
        flag,
        canteenId
      } = res.data
      wx.setStorageSync('userToken', token)
      wx.setStorageSync('userId', userid)
      wx.setStorageSync('userRole', flag)
      wx.setStorageSync('realName', name)
      wx.setStorageSync('supplierName', supplierName)
      wx.setStorageSync('userAccount', username)
      wx.setStorageSync('departmentType', canteenId ==="1067246875800000066"?1:0)
      this.toHome(flag)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      
  },  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
  toHome(flag) {
    if (flag === 2) {
      //食堂主厨
      wx.redirectTo({
        url: '/pages/chef-home/chef-home',
      })
    } else if (flag === 3) {
      //食堂管理员
      wx.redirectTo({
        url: '/pages/admin-home/admin-home',
      })
    } else if (flag === 5) {
      //仓储员
      wx.redirectTo({
        url: '/pages/warehouse-home/warehouse-home',
      })
    } else if (flag === 4) {
      wx.redirectTo({
        url: '/pages/supplier-home/supplier-home',
      })
    } else if (flag === 7) {
      wx.redirectTo({
        url: '/pages/regulation-home/regulation-home',
      })
    } else if (flag === 6) {
      wx.redirectTo({
        url: '/pages/finance-home/finance-home',
      })
    } else if (flag === 8) {
      wx.redirectTo({
        url: '/pages/purchaser-home/purchaser-home',
      })
    }
  },
})